﻿using System;
using System.Collections;
using System.Data.SqlClient;
using System.Windows;
using System.Windows.Controls;
using Tools;
using WPF数据采集系统.Base;
using WPF数据采集系统.Model;
using WPF数据采集系统.ViewModel;

namespace WPF数据采集系统.View
{
    /// <summary>
    /// PlcView.xaml 的交互逻辑
    /// </summary>
    public partial class PlcView : UserControl
    {
        PlcViewModel pvm = new PlcViewModel();

        public PlcView()
        {
            InitializeComponent();
        }

        private void btn_read_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btn_write_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btn_save_Click(object sender, RoutedEventArgs e)
        {
            //PlcViewModel pvm = new PlcViewModel();
            //PlcModel pm = pvm.pm;
            //pm.PlcIp = plc_ip.Text.Trim();
            //pm.LocalIp = local_ip.Text.Trim();
            //pm.PlcType = plc_type.Text.Trim();
            //pm.LocalPort = int.Parse(local_port.Text.Trim());
            //pm.PlcPort = int.Parse(remo_port.Text.Trim());
            //pm.ReadAddress = int.Parse(read_path.Text.Trim());
            //pm.ReadLength = int.Parse(read_count.Text.Trim());
            //pm.ReadType = read_type.Text.Trim();
            //pm.WriteAddress = int.Parse(write_path.Text.Trim());
            //pm.WriteLength = int.Parse(write_count.Text.Trim());
            //pm.WriteData = write_content.Text.Trim();
            //pm.WriteType = write_type.Text.Trim();
            //pm.AutoConnect = (bool)plc_checkBox.IsChecked;
            ////PlcStateMonitor.plc.localPort = int.Parse(pm.LocalPort);
            ////PlcStateMonitor.plc.remotoPort = int.Parse(pm.PlcPort);
            //try
            //{
            //    pvm.SavePlcData(pm);
            //    ListBoxHelper.listBoxAdd(MainWindow.mvm.logListBox, "用户"+MainWindow.LoginUser+"点击了PLC设置页面的保存按钮,保存成功!", true);
            //}
            //catch (Exception ex)
            //{
            //    string mesage = "错误消息:" + ex.Message + " 导致异常的实例:" + ex.InnerException + " 对象名:" + ex.Source + " 函数名:" + ex.TargetSite;
            //    ListBoxHelper.listBoxAdd(MainWindow.mvm.logListBox, mesage, true);
            //}
        }

        private void btn_connect_Click(object sender, RoutedEventArgs e)
        {
            PlcStateMonitor psm = new PlcStateMonitor();
            if (MainWindow.mvm.PlcState)
            {
                if(CustomMsgBox.ShowDialog("Warning", "正在与PLC通讯中,是否确认要断开连接?(In communication with PLC, are you sure you want to disconnect?)", CustomMsgBoxButton.Two,CustomMsgBoxImage.Alert).IsYes)
                {
                    MainWindow.mvm.PlcState = false;
                    PlcStateMonitor.PlcContent = "Error";

                    ListBoxHelper.listBoxAdd(MainWindow.mvm.logListBox, "用户:" + MainWindow.LoginUser + "断开了PLC连接", true);
                }
            }
            else
            {
                MainWindow.mvm.PlcState = true;
                psm.MainRun(true);
                ListBoxHelper.listBoxAdd(MainWindow.mvm.logListBox, "用户:" + MainWindow.LoginUser + "点击了PLC页面的连接按钮", true);
            }
        }

        private void plc_ip_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            CheckBox cb = sender as CheckBox;
            if ((bool)cb.IsChecked)
            {
                cb.Content = "(Enabled)已启用";
            }
            else
            {
                cb.Content = "(Disabled)已禁用";
            }
        }
        private void CheckBox_Loaded(object sender, RoutedEventArgs e)
        {
            CheckBox cb = sender as CheckBox;
            if ((bool)cb.IsChecked)
            {
                cb.Content = "(Enabled)已启用";
            }
            else
            {
                cb.Content = "(Disabled)已禁用";
            }
        }
        private void CheckBox_Loaded1(object sender, RoutedEventArgs e)
        {
            CheckBox cb = sender as CheckBox;
            if ((bool)cb.IsChecked)
            {
                cb.Content = "(Auto connection)自动连接";
            }
            else
            {
                cb.Content = "(Manual connection)手动连接";
            }
        }
        private void CheckBox_Click1(object sender, RoutedEventArgs e)
        {
            CheckBox cb = sender as CheckBox;
            if ((bool)cb.IsChecked)
            {
                cb.Content = "(Auto connection)自动连接";
            }
            else
            {
                cb.Content = "(Manual connection)手动连接";
            }
        }
        private void CheckBox_Loaded2(object sender, RoutedEventArgs e)
        {
            CheckBox cb = sender as CheckBox;
            if ((bool)cb.IsChecked)
            {
                cb.Content = "(Core PLC)核心PLC";
            }
            else
            {
                cb.Content = "(Non core)非核心";
            }
        }
        private void CheckBox_Click2(object sender, RoutedEventArgs e)
        {
            CheckBox cb = sender as CheckBox;
            if ((bool)cb.IsChecked)
            {
                cb.Content = "(Manual connection)核心PLC";
            }
            else
            {
                cb.Content = "(Manual connection)非核心";
            }
        }

        private void Add_Click(object sender, RoutedEventArgs e)
        {
            PlcModel pm = new PlcModel();
            pvm.plcInfoList.Add(pm);
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            plc_list_view.ItemsSource = pvm.plcInfoList;
        }

        private void Delete_Click(object sender, RoutedEventArgs e)
        {
            int index = plc_list_view.SelectedIndex;
            pvm.plcInfoList.RemoveAt(index);
        }
        private void Edit_Click(object sender, RoutedEventArgs e)
        {
        }

        private void Button_Click(object sender, RoutedEventArgs e)
       {
            Button btn = sender as Button;
            var v = plc_list_view.ItemContainerStyle.Setters[8].IsSealed;
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            string del = "Delete PlcInfo";
            SqlHelper.Update(del);
            foreach (PlcModel pm in plc_list_view.Items)
            {              
                string sql = "update PlcInfo set plc_ip=@pip,plc_port=@pp,plc_local_ip=@pli,plc_local_port=@plp,plc_auto_connect=@pac,plc_usability=@pu, plc_core=@pc where plc_id=@pi";
                //plc_ip=@pip,plc_port=@pp,plc_local_ip=@pli,plc_local_port=@plp,plc_auto_connect=@pac,plc_usability=@pu,
                SqlParameter[] param1 = new SqlParameter[]
                {
                new SqlParameter("@pi",pm.PlcId),
                new SqlParameter("@pip",pm.PlcIp),
                new SqlParameter("@pp",pm.PlcPort),
                new SqlParameter("@pli",pm.LocalIp),
                new SqlParameter("@plp",pm.LocalPort),
                new SqlParameter("@pac",pm.AutoConnect),
                new SqlParameter("@pu",pm.Usability),
                new SqlParameter("@pc",pm.Core)
                };
                try
                {
                    int i = SqlHelper.Update(sql, param1);
                    if (i == 0)
                    {
                        string insert = "insert into PlcInfo(plc_id,plc_ip,plc_port,plc_local_ip,plc_local_port,plc_auto_connect,plc_usability,plc_core) values(@pi,@pip,@pp,@pli,@plp,@pac,@pu,@pc)";
                        SqlParameter[] insertParam = new SqlParameter[]
                        {
                        new SqlParameter("@pi",pm.PlcId),
                        new SqlParameter("@pip",pm.PlcIp),
                        new SqlParameter("@pp",pm.PlcPort),
                        new SqlParameter("@pli",pm.LocalIp),
                        new SqlParameter("@plp",pm.LocalPort),
                        new SqlParameter("@pac",pm.AutoConnect),
                        new SqlParameter("@pu",pm.Usability),
                        new SqlParameter("@pc",pm.Core)
                         };
                        i = SqlHelper.Update(insert, insertParam);
                    }
                    ListBoxHelper.listBoxAdd(MainWindow.mvm.logListBox, "修改PLC信息成功,PLC ID:" + pm.PlcId, true);

                }
                catch (Exception ex)
                {
                    ListBoxHelper.listBoxAdd(MainWindow.mvm.logListBox, ex.Message, true);
                }
            }
        }
    }
}
